const path = require('path')
const webpack = require('webpack')

const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractPlugin = require('extract-text-webpack-plugin')

const isDev = process.env.NODE_ENV === 'development'

const config = {
  target: 'web',
  entry: path.join(__dirname, 'src/index.js'),
  output: {
    filename: 'bundle.[hash:8].js',
    path: path.join(__dirname, 'dist')
  },
  plugins: [
    // make sure to include the plugin for the magic
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: isDev ? '"development"' : '"production"'
      }
    }),
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin()
  ],
  module: {
    rules: [
      {
        test: /.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /.jsx$/,
        loader: 'babel-loader'
      },
      {
        test: /\.(gif|jpg|jpeg|png|svg)$/,
        loader: 'url-loader',
        options: {
          limit: 102400,
          name: '[name].[ext]'
        }
      }
    ]
  }
}

if (isDev) {
  config.module.rules.push({
    test: /\.styl(us)?$/,
    loader: 'style-loader!css-loader!stylus-loader'
  })
  config.devtool = '#cheap-module-eval-source-map'
  config.devServer = {
    port: '8000',
    host: '0.0.0.0',
    overlay: {
      errors: true
    },
    hot: true
  }
  config.plugins.push(
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  )
} else {
  config.entry = {
    app: path.join(__dirname, 'src/index.js'),
    vendor: ['vue']
  }
  config.output.filename = '[name].[chunkhash:8].js'
  config.module.rules.push(
    {
      test: /\.styl(us)?$/,
      use: ExtractPlugin.extract({
          fallback: 'style-loader',
          use: [
              'css-loader',
              {
                  loader: 'postcss-loader',
                  options: {
                      sourceMap: true
                  }
              },
              'stylus-loader'
          ]
      })
    }
  )
  config.plugins.push(
    new ExtractPlugin('styles.css')
  )
  config.optimization = {
    splitChunks: {
        cacheGroups: {                  // 这里开始设置缓存的 chunks
            commons: {
                chunks: 'initial',      // 必须三选一： "initial" | "all" | "async"(默认就是异步)
                minSize: 0,             // 最小尺寸，默认0,
                minChunks: 2,           // 最小 chunk ，默认1
                maxInitialRequests: 5   // 最大初始化请求数，默认1
            },
            vendor: {
                test: /node_modules/,   // 正则规则验证，如果符合就提取 chunk
                chunks: 'initial',      // 必须三选一： "initial" | "all" | "async"(默认就是异步)
                name: 'vendor',         // 要缓存的 分隔出来的 chunk 名称
                priority: 10,           // 缓存组优先级
                enforce: true
            }
        }
    },
    runtimeChunk: true
}
}

module.exports = config